package com.ophelisis.actions.boardMgmt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ophelisis.persistence.dao.BoardCalendarDAO;
import com.ophelisis.persistence.domain.BoardCalendarVO;

/** <pre>
 * Class : com.ophelisis.actions.boardMgmt.CalendarMgmt / CalendarMgmt.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hlee / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2013. 07. 29.
 */
@Controller
public class CalendarMgmt {
	@Autowired
	BoardCalendarDAO boardCalendarDAO;
	
	private List<BoardCalendarVO> calendarList;
	private String sResult = "";
	
	private Log log = LogFactory.getLog(this.getClass());
	private static final String _LogPrefixStr = "BoardMgmt Log> ";
	
	@RequestMapping(value="boardCalendarMgmt.action")
	public ModelAndView boardCalendarMgmt(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="target", defaultValue="bmMain") String sTarget,
			@RequestParam(value="_groupId", defaultValue="0") String sMenuGroupId,
			@RequestParam(value="_menuId", defaultValue="0") String sMenuId,
			@RequestParam(value="_menuType", defaultValue="1") String sMenuType,
			@RequestParam(value="_menuNm", defaultValue="") String sMenuNm,
			@RequestParam(value="_menuAuth", defaultValue="0") String sUserMenuAuth,
			@RequestParam(value="_prevUpCode", defaultValue="") String sPrevUpCode,
			@RequestParam(value="_prevCode", defaultValue="") String sPrevCode,
			@RequestParam(value="_year", defaultValue="") String sYear,
			@RequestParam(value="_month", defaultValue="") String sMonth,
			@RequestParam(value="_startDay", defaultValue="") String sStartDay,
			@RequestParam(value="_endDay", defaultValue="") String sEndDay,
			@RequestParam(value="calendarType", defaultValue="month") String sCalendarType,
			@RequestParam(value="isNew", defaultValue="Y") String isNew,
			@ModelAttribute BoardCalendarVO boardCalendarVO) throws Exception {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession(false);

		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		log.debug(_LogPrefixStr + "|" + " CalendarMgmt called !!!");
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");

		try {
			// 공통 요소
			mav.addObject("_groupId", sMenuGroupId);
			mav.addObject("_menuId", sMenuId);
			mav.addObject("_menuType", sMenuType);
			mav.addObject("_menuNm", sMenuNm);
			mav.addObject("_menuAuth", sUserMenuAuth);
			mav.addObject("_prevUpCode", sPrevUpCode);
			mav.addObject("_prevCode", sPrevCode);
			mav.addObject("_year", sYear);
			mav.addObject("_month", sMonth);
			mav.addObject("_startDay", sStartDay);
			mav.addObject("_endDay", sEndDay);
			mav.addObject("calendarType", sCalendarType);
			
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("menuGroupId", sMenuGroupId);
			params.put("menuId", sMenuId);
			params.put("menuType", sMenuType);
			params.put("year", sYear);
			params.put("month", sMonth);
			params.put("startDay", sStartDay);
			params.put("endDay", sEndDay);
			params.put("calendarType", sCalendarType);
			params.put("calendarId", sPrevCode);
			params.put("updateUser", (String)session.getAttribute("userId"));

			if (sTarget.equals("bmMain")) {
				mav = returnMgmt("main", mav, params);
			} else if (sTarget.equals("bmList")) {
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("bmDetail")) {
				mav = returnMgmt("detail", mav, params);
			} else if (sTarget.equals("bmAdd")) {
				mav.setViewName("/WEB-INF/jsp/boardMgmt/calendarMgmtAdd.jsp");
				
				log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
				log.debug(_LogPrefixStr + "|" + " CalendarMgmt Add-Page called !!!");
				log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			} else if (sTarget.equals("bmDelete")) {
				try {
					sResult = boardCalendarDAO.setBoardCalendarDelete(params) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 유저 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!", e);
				}
				
				mav.addObject("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			} else if (sTarget.equals("bmSave")) {
				boardCalendarVO.setMenuId(sMenuId);
				boardCalendarVO.setMenuType(sMenuType);
				boardCalendarVO.setUpdateUser((String)session.getAttribute("userId"));

				String sStartDate = boardCalendarVO.getStartDate();
				String sEndDate = boardCalendarVO.getEndDate();

				boardCalendarVO.setStartYear(sStartDate.substring(0, 4));
				boardCalendarVO.setStartMonth(sStartDate.substring(5, 7));
				boardCalendarVO.setStartDay(sStartDate.substring(8, 10));
				boardCalendarVO.setEndYear(sEndDate.substring(0, 4));
				boardCalendarVO.setEndMonth(sEndDate.substring(5, 7));
				boardCalendarVO.setEndDay(sEndDate.substring(8, 10));
				try {
					sResult = (isNew.equals("Y") ? boardCalendarDAO.setBoardCalendarAdd(boardCalendarVO) : boardCalendarDAO.setBoardCalendarModify(boardCalendarVO)) > 0 ? "정상적으로 처리되었습니다." : "처리에 실패하였습니다.<BR/>관리자에게 문의하여 주십시오."; // 정보 업데이트
				} catch (Exception e) {
					log.error("DB 정보 획득 failed!", e);
				}
				request.setAttribute("result", sResult);
				
				mav = returnMgmt("list", mav, params);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}

		return mav;
	}
	
	public ModelAndView returnMgmt(
			@RequestParam(value="cmd", defaultValue="list") String cmd,
			@ModelAttribute ModelAndView mav,
			Map<String, Object> params) throws Exception {
		if (cmd.equals("main")) {
			mav.setViewName("/WEB-INF/jsp/boardMgmt/calendarMgmt.jsp");

			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " CalendarMgmt Main-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		} else if (cmd.equals("list")) {
			try {
				calendarList = boardCalendarDAO.getBoardCalendarList(params); // 쿼리 결과
			} catch (Exception e) {
				log.error("DB 정보 획득 failed!", e);
			}

			mav.addObject("calendarList", calendarList);
			mav.setViewName("/WEB-INF/jsp/boardMgmt/calendarMgmtList.jsp");
			
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " CalendarMgmt List-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		} else if (cmd.equals("detail")) {
			try {
				calendarList = boardCalendarDAO.getBoardCalendarDetail(params); // 쿼리 결과
			} catch (Exception e) {
				log.error("DB 정보 획득 failed!", e);
			}

			mav.addObject("calendarList", calendarList);
			mav.setViewName("/WEB-INF/jsp/boardMgmt/calendarMgmtDetail.jsp");
			
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
			log.debug(_LogPrefixStr + "|" + " CalendarMgmt Detail-Page called !!!");
			log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		}
		
		return mav;
	}
	
	@RequestMapping(value="calendarEvent.action")
	public ModelAndView calendarEvent(
			@RequestParam(value="_menuId", defaultValue="0") String sMenuId,
			@RequestParam(value="_menuType", defaultValue="1") String sMenuType,
			@RequestParam(value="_year", defaultValue="") String sYear,
			@RequestParam(value="_month", defaultValue="") String sMonth,
			@RequestParam(value="_startDay", defaultValue="") String sStartDay,
			@RequestParam(value="_endDay", defaultValue="") String sEndDay,
			@RequestParam(value="calendarType", defaultValue="month") String sCalendarType) throws Exception {
		ModelAndView mav = new ModelAndView();

		// 공통 요소
		mav.addObject("_menuId", sMenuId);
		mav.addObject("_menuType", sMenuType);
		mav.addObject("_year", sYear);
		mav.addObject("_month", sMonth);
		mav.addObject("_startDay", sStartDay);
		mav.addObject("_endDay", sEndDay);
		mav.addObject("calendarType", sCalendarType);
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("menuId", sMenuId);
		params.put("menuType", sMenuType);
		params.put("year", sYear);
		params.put("month", sMonth);
		params.put("startDay", sStartDay);
		params.put("endDay", sEndDay);
		params.put("calendarType", sCalendarType);
		
		try {
			calendarList = boardCalendarDAO.getBoardCalendarList(params); // 쿼리 결과
		} catch (Exception e) {
			log.error("DB 정보 획득 failed!", e);
		}

		mav.addObject("calendarList", calendarList);
		mav.setViewName("/WEB-INF/jsp/boardMgmt/calendarMgmtEvent.jsp");
		
		return mav;
	}
}

